﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Common;

using System.ComponentModel;

using System.Data.Linq;
using System.Data.Linq.Mapping;

using System.Linq.Expressions;
using System.Reflection;

public struct DepartmentDetails
{
    public string Name;
    public string Description;
}

/// <summary>
/// Wraps category details data
/// </summary>
public struct CategoryDetails
{
    public int DepartmentId;
    public string Name;
    public string Description;
}

/// <summary>
/// Wraps product details data
/// </summary>
public struct ProductDetails
{
    public int ProductID;
    public string Name;
    public string Description;
    public decimal Price;
    public string Thumbnail;
    public string Image;
    public bool PromoFront;
    public bool PromoDept;
}
/// <summary>
///CatalogAccess 的摘要说明
/// </summary>
public static class CatalogAccess
{
    static CatalogAccess()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    // DataClassesDataContext da = new DataClassesDataContext();
    public static ISingleResult<CatalogGetDepartmentsResult> GetDepartments()
    {
        DataClassesDataContext da = new DataClassesDataContext();
        ISingleResult<CatalogGetDepartmentsResult> dtbl = da.CatalogGetDepartments();

        return dtbl;
    }
    public static DepartmentDetails GetDepartmentDetails(string departmentId)
    {
        DataClassesDataContext da = new DataClassesDataContext();
        ISingleResult<CatalogGetDepartmentDetailsResult> table = da.CatalogGetDepartmentDetails(Int32.Parse(departmentId));
        DepartmentDetails details = new DepartmentDetails();
        List<CatalogGetDepartmentDetailsResult> table1 = table.ToList();
        details.Name = table1.FirstOrDefault().Name.ToString();
        details.Description = table1.FirstOrDefault().Description.ToString();

        // return department details
        return details;
    }
    public static CategoryDetails GetCategoryDetails(string categoryId)
    {
        DataClassesDataContext da = new DataClassesDataContext();
        ISingleResult<CatalogGetCategoryDetailsResult> table = da.CatalogGetCategoryDetails(Int32.Parse(categoryId));
        CategoryDetails details = new CategoryDetails();
        List<CatalogGetCategoryDetailsResult> table1 = table.ToList();
        if (table1.Count() > 0)
        {
            details.Name = table1.ElementAt(0).Name.ToString();
            details.Description = table1.ElementAt(0).Description.ToString();
        }
        // return department details
        return details;
    }
    public static ProductDetails GetProductDetails(string productId)
    {
        if (productId == null)
        {
            productId = "1";
        }
        DataClassesDataContext da = new DataClassesDataContext();
        ISingleResult<CatalogGetProductDetailsResult> table = da.CatalogGetProductDetails(Int32.Parse(productId));
        ProductDetails details = new ProductDetails();
        List<CatalogGetProductDetailsResult> table1 = table.ToList();
        if (table1.Count() > 0)
        {
            details.ProductID = int.Parse(productId);
            details.Name = table1.ElementAt(0).Name.ToString();
            details.Description = table1.ElementAt(0).Description.ToString();
            details.Price = Decimal.Parse(table1.ElementAt(0).Price.ToString());
            details.Thumbnail = table1.ElementAt(0).Thumbnail.ToString();
            details.Image = table1.ElementAt(0).Image.ToString();
            details.PromoFront = bool.Parse(table1.ElementAt(0).PromoFront.ToString());
            details.PromoDept = bool.Parse(table1.ElementAt(0).PromoDept.ToString());

        }

        // return department details
        return details;

    }
    public static ISingleResult<CatalogGetCategoriesInDepartmentResult> GetCategoriesInDepartment(string departmentId)
    {
        DataClassesDataContext da = new DataClassesDataContext();
        ISingleResult<CatalogGetCategoriesInDepartmentResult> table = da.CatalogGetCategoriesInDepartment(Int32.Parse(departmentId));
        return table;
    }
    public static ISingleResult<CatalogGetProductsOnFrontPromoResult> GetProductsOnFrontPromo(string pageNumber, out int howManyPages)
    {
        DataClassesDataContext da = new DataClassesDataContext();
        int? HowManyProducts = null;

        ISingleResult<CatalogGetProductsOnFrontPromoResult> table = da.CatalogGetProductsOnFrontPromo(BalloonShopConfiguration.ProductDescriptionLength, Int32.Parse(pageNumber), BalloonShopConfiguration.ProductsPerPage, ref HowManyProducts);
        int howManyProducts = HowManyProducts.Value;
        howManyPages = (int)Math.Ceiling((double)howManyProducts / (double)BalloonShopConfiguration.ProductsPerPage);
        // return the page of products
        return table;
    }
    public static ISingleResult<CatalogGetProductsOnDeptPromoResult> GetProductsOnDeptPromo(string departmentId, string pageNumber, out int howManyPages)
    {
        DataClassesDataContext da = new DataClassesDataContext();
        int? HowManyProducts = null;
        ISingleResult<CatalogGetProductsOnDeptPromoResult> table = da.CatalogGetProductsOnDeptPromo(Int32.Parse(departmentId), BalloonShopConfiguration.ProductDescriptionLength, Int32.Parse(pageNumber), BalloonShopConfiguration.ProductsPerPage, ref HowManyProducts);
        int howManyProducts = HowManyProducts.Value;
        howManyPages = (int)Math.Ceiling((double)howManyProducts / (double)BalloonShopConfiguration.ProductsPerPage);
        return table;
    }
    public static ISingleResult<CatalogGetProductsInCategoryResult> GetProductsInCategory(string categoryId, string pageNumber, out int howManyPages)
    {
        DataClassesDataContext da = new DataClassesDataContext();
        int? HowManyProducts = null;
        ISingleResult<CatalogGetProductsInCategoryResult> table = da.CatalogGetProductsInCategory(Int32.Parse(categoryId), BalloonShopConfiguration.ProductDescriptionLength, Int32.Parse(pageNumber), BalloonShopConfiguration.ProductsPerPage, ref HowManyProducts);
        int howManyProducts = HowManyProducts.Value;
        howManyPages = (int)Math.Ceiling((double)howManyProducts / (double)BalloonShopConfiguration.ProductsPerPage);
        return table;
    }
    public static ISingleResult<CatalogGetProductAttributeValuesResult> GetProductAttributes(string productId)
    {
        if (productId == "")
        {
            productId = "1";
        }
        DataClassesDataContext da = new DataClassesDataContext();
        ISingleResult<CatalogGetProductAttributeValuesResult> table = da.CatalogGetProductAttributeValues((Int32.Parse(productId)));
        return table;
    }
    public static bool UpdateDepartment(string id, string name, string description)
    {
        int result = -1;
        DataClassesDataContext da = new DataClassesDataContext();
        try
        {
            // execute the stored procedure
            result = da.CatalogUpdateDepartment(Int32.Parse(id), name, description);
        }
        catch
        {
            // any errors are logged in GenericDataAccess, we ignore them here
        }
        // result will be 1 in case of success 
        return (result != -1);
    }
    public static bool DeleteDepartment(string id)
    {
        int result = -1;
        DataClassesDataContext da = new DataClassesDataContext();
        try
        {
            // execute the stored procedure
            result = da.CatalogDeleteDepartment(Int32.Parse(id));
        }
        catch
        {
            // any errors are logged in GenericDataAccess, we ignore them here
        }
        // result will be 1 in case of success 
        return (result != -1);
    }
    public static bool AddDepartment(string name, string description)
    {
        int result = -1;
        DataClassesDataContext da = new DataClassesDataContext();
        try
        {
            // execute the stored procedure
            result = da.CatalogAddDepartment(name, description);
        }
        catch
        {
            // any errors are logged in GenericDataAccess, we ignore them here
        }
        // result will be 1 in case of success 
        return (result != -1);
    }
    public static bool CreateCategory(string departmentId,
     string name, string description)
    {
        int result = -1;
        DataClassesDataContext da = new DataClassesDataContext();
        try
        {
            // execute the stored procedure
            result = da.CatalogCreateCategory(Int32.Parse(departmentId), name, description);
        }
        catch
        {
            // any errors are logged in GenericDataAccess, we ignore them here
        }
        // result will be 1 in case of success 
        return (result != -1);
    }
    public static bool UpdateCategory(string id, string name, string description)
    {
        int result = -1;
        DataClassesDataContext da = new DataClassesDataContext();
        try
        {
            // execute the stored procedure
            result = da.CatalogUpdateCategory(Int32.Parse(id), name, description);
        }
        catch
        {
            // any errors are logged in GenericDataAccess, we ignore them here
        }
        // result will be 1 in case of success 
        return (result != -1);
    }
    public static bool DeleteCategory(string id)
    {
        int result = -1;
        DataClassesDataContext da = new DataClassesDataContext();
        try
        {
            // execute the stored procedure
            result = da.CatalogDeleteCategory(Int32.Parse(id));
        }
        catch
        {
            // any errors are logged in GenericDataAccess, we ignore them here
        }
        // result will be 1 in case of success 
        return (result != -1);
    }
    public static ISingleResult<CatalogGetAllProductsInCategoryResult> GetAllProductsInCategory(string categoryId)
    {
        DataClassesDataContext da = new DataClassesDataContext();
        ISingleResult<CatalogGetAllProductsInCategoryResult> table = da.CatalogGetAllProductsInCategory(Int32.Parse(categoryId));
        return table;
    }
    public static bool CreateProduct(string categoryId, string name, string description, string price, string Thumbnail, string Image, bool PromoDept, bool PromoFront)
    {
        DataClassesDataContext da = new DataClassesDataContext();
       
        try
        {
            // execute the stored procedure
             da.CatalogCreateProduct(Int32.Parse(categoryId), name, description, Convert.ToDecimal(price), Thumbnail, Image, PromoDept, PromoFront);
             return true;
        }
        catch
        {
            return false;
            // any errors are logged in GenericDataAccess, we ignore them here
        }
        // result will be 1 in case of success 
      // return false;
    }
    public static bool UpdateProduct(string productId, string name, string description, string price, string Thumbnail, string Image, bool PromoDept, bool PromoFront)
    {
        DataClassesDataContext da = new DataClassesDataContext();
        int result = -1;
        try
        {
            // execute the stored procedure
            result = da.CatalogUpdateProduct(Int32.Parse(productId), name, description, Convert.ToDecimal(price), Thumbnail, Image, PromoDept, PromoFront);
        }
        catch
        {
            // any errors are logged in GenericDataAccess, we ignore them here
        }
        // result will be 1 in case of success 
        return (result >= 1);
    }
    public static ISingleResult<CatalogGetCategoriesWithProductResult> GetCategoriesWithProduct(string productId)
    {
        DataClassesDataContext da = new DataClassesDataContext();
        return da.CatalogGetCategoriesWithProduct(Int32.Parse(productId));
    }
    public static ISingleResult<CatalogGetCategoriesWithoutProductResult> GetCategoriesWithoutProduct(string productId)
    {
        DataClassesDataContext da = new DataClassesDataContext();
        return da.CatalogGetCategoriesWithoutProduct(Int32.Parse(productId));
    }
    public static bool AssignProductToCategory(string productId, string categoryId)
    {
        DataClassesDataContext da = new DataClassesDataContext();
        int result = -1;
        try
        {
            // execute the stored procedure
            result = da.CatalogAssignProductToCategory(Int32.Parse(productId), Int32.Parse(categoryId));
        }
        catch
        {
            // any errors are logged in GenericDataAccess, we ignore them here
        }
        // result will be 1 in case of success 
        return (result != -1);
    }
    public static bool MoveProductToCategory(string productId, string oldCategoryId,
     string newCategoryId)
    {
        DataClassesDataContext da = new DataClassesDataContext();
        int result = -1;
        try
        {
            // execute the stored procedure
            result = da.CatalogMoveProductToCategory(Int32.Parse(productId), Int32.Parse(oldCategoryId), Int32.Parse(newCategoryId));
        }
        catch
        {
            // any errors are logged in GenericDataAccess, we ignore them here
        }
        // result will be 1 in case of success 
        return (result != -1);
    }
    public static bool RemoveProductFromCategory(string productId, string categoryId)
    {
        DataClassesDataContext da = new DataClassesDataContext();
        int result = -1;
        try
        {
            // execute the stored procedure
            result = da.CatalogRemoveProductFromCategory(Int32.Parse(productId), Int32.Parse(categoryId));
        }
        catch
        {
            // any errors are logged in GenericDataAccess, we ignore them here
        }
        // result will be 1 in case of success 
        return (result != -1);
    }
    public static bool DeleteProduct(string productId)
    {
        DataClassesDataContext da = new DataClassesDataContext();
        int result = -1;
        try
        {
            // execute the stored procedure
            result = da.CatalogDeleteProduct(Int32.Parse(productId));
        }
        catch
        {
            // any errors are logged in GenericDataAccess, we ignore them here
        }
        // result will be 1 in case of success 
        return (result != -1);
    }
    public static ISingleResult<StoredProcedure1Result> search(string word)
    {
        DataClassesDataContext da = new DataClassesDataContext();

        return   da.StoredProcedure1(word);
    }
}